﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudentMS
{
    public partial class EditStudentForm : Form
    {
        public EditStudentForm()
        {
            InitializeComponent();
        }

        private int stuId = 0;
        public EditStudentForm(int stuId)
        {
            InitializeComponent();
            this.stuId = stuId;
        }

        private Action reLoad = null;

        private void InitClass()
        {
            string sql = "select ClassId,ClassName,GradeName from ClassInfo c,GradeInfo g where c.GradeId=g.GradeId";
            DataTable dataTable = SqlHelper.GetDataTable(sql);
            if (dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    string ClassName = row["ClassName"].ToString();
                    string gradeName = row["GradeName"].ToString();
                    row["ClassName"] = ClassName + "--" + gradeName;
                }
            }
            cboClasses.DataSource = dataTable;
            cboClasses.DisplayMember = "ClassName";
            cboClasses.ValueMember = "ClassId";
            cboClasses.SelectedIndex = 0;
        }

        private void EditStudentForm_Load(object sender, EventArgs e)
        {
            InitClass();
            InitStudentInfo();
        }

        private void InitStudentInfo()
        {
            if (this.Tag != null)
            {
                TagObject tagObject = (TagObject)this.Tag;
                stuId = tagObject.EditId;
                reLoad = tagObject.ReLoad;
            }
            string sql = "select StuName,Sex,ClassId,Phone from StudentInfo where StuId=@StuId";
            SqlParameter paraId = new SqlParameter("@StuId", stuId);
            SqlDataReader reader = SqlHelper.ExecuteReader(sql, paraId);
            if (reader.Read())
            {
                txtStuName.Text = reader["StuName"].ToString();
                txtPhone.Text = reader["Phone"].ToString();
                string sex = reader["Sex"].ToString();
                if (sex == "男")
                    rbtMale.Checked = true;
                else
                    rbtFemale.Checked = true;
                int classId = (int)reader["ClassId"];
                cboClasses.SelectedValue = classId;
            }
            reader.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string stuName = txtStuName.Text.Trim();
            int classId = (int)cboClasses.SelectedValue;
            string sex = rbtMale.Checked ? rbtMale.Text.Trim() : rbtFemale.Text.Trim();
            string phone = txtPhone.Text.Trim();

            if (string.IsNullOrEmpty(stuName))
            {
                MessageBox.Show("学生姓名不能为空！", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(phone))
            {
                MessageBox.Show("电话不能为空！", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string sql = "select count(1) from StudentInfo where StuName=@StuName and Phone=@Phone and StuId<>@StuId";
            SqlParameter[] paras =
            {
                new SqlParameter("@StuName", stuName),
                new SqlParameter("@Phone", phone),
                new SqlParameter("StuId", stuId)
            };
            object obj = SqlHelper.ExecuteScalar(sql, paras);
            if (obj != null && obj != DBNull.Value && ((int)obj) > 0)
            {
                MessageBox.Show("该学生已存在！", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string sqlUpdate = "update StudentInfo set StuName=@StuName,Sex=@Sex,ClassId=@ClassId,Phone=@Phone " +
                "where StuId=@StuId";
            SqlParameter[] parasEdit =
            {
                    new SqlParameter("@StuName", stuName),
                    new SqlParameter("@ClassId", classId),
                    new SqlParameter("@Sex", sex),
                    new SqlParameter("@Phone", phone),
                    new SqlParameter("@StuId", stuId)
                };
            int count = SqlHelper.ExecuteNonQuery(sqlUpdate, parasEdit);
            if (count > 0)
            {
                MessageBox.Show($"学生：{stuName}修改成功!", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reLoad.Invoke();
            }
            else
            {
                MessageBox.Show("该学生修改失败，请检查！", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
